#include "fighter.h"


#include "CharacterJabRight.h"
#include "CharacterIdle.h"
#include "CharacterJabLeft.h"
#include "CharacterHookRight.h"
#include "CharacterHookLeft.h"
#include "CharacterDodgeRight.h"
#include "CharacterDodgeLeft.h"
#include "CharacterHurt.h"
#include "CharacterFlinch.h"

fighter::fighter()
	: mpCurrentState(NULL)
	, mPosition(0.0f, 0.0f)
	, CurState(0)
	, mFacingLeft(false)
	
{
	memset(mCharacterStates, 0, sizeof(CharacterState*) * AS_Max);
}

fighter::~fighter()
{
}

void fighter::Load(const char* pfighter)
{
	// Clear everything
	Unload();

	// Create character states
	mCharacterStates[AS_Idle] = new CharacterIdle(this);
	mCharacterStates[AS_JabRight] = new CharacterJabRight(this);
	mCharacterStates[AS_JabLeft] = new CharacterJabLeft(this);
	mCharacterStates[AS_DodgeRight] = new CharacterDodgeRight(this);
	mCharacterStates[AS_DodgeLeft] = new CharacterDodgeLeft(this);
	mCharacterStates[AS_HookRight] = new CharacterHookRight(this);
	mCharacterStates[AS_HookLeft] = new CharacterHookLeft(this);
	mCharacterStates[AS_Hurt] = new CharacterHurt(this);
	mCharacterStates[AS_Flinch] = new CharacterFlinch(this);

	FILE* pFile = NULL;
	fopen_s(&pFile, pfighter, "r");

	// Load state resources
	for (int i = 0; i < AS_Max; ++i)
	{
		mCharacterStates[i]->Load(pFile);
	}

	fclose(pFile);
	// Set default state
	ChangeState(AS_Idle);
}

void fighter::Unload()
{
	for (int i = 0; i < AS_Max; ++i)
	{
		if (mCharacterStates[i] != NULL)
		{
			mCharacterStates[i]->Unload();
			delete mCharacterStates[i];
			mCharacterStates[i] = NULL;
		}
	}
	mpCurrentState = NULL;
}

void fighter::Update(float deltaTime)
{
	// Update the current state
	mpCurrentState->Update(deltaTime);
	SRect boundingBox = GetBoundingBox();
	SRect DamageBox = GetDamageBox();
	
}

void fighter::Render(const SVector2& offset)
{
	mpCurrentState->Render(offset);	
}

void fighter::ChangeState(AnimationState as)
{
	// Check if we are already in this state
	if (mpCurrentState == mCharacterStates[as])
	{
		return;
	}

	// Exit the current state
	if (mpCurrentState != NULL)
	{
		mpCurrentState->Exit();
	}

	// Switch to the new state
	mpCurrentState = mCharacterStates[as];

	CurState = as;

	// Enter the new state
	if (mpCurrentState != NULL)
	{
		mpCurrentState->Enter();
	}
}

const char* fighter::GetCurrentState()
{
	const char* name = "Unknown";
	if (mpCurrentState != NULL)
	{
		name = mpCurrentState->GetName();
	}
	return name;
}

//----------------------------------------------------------------------------------------------------

int fighter::GetIntState()
{
	return CurState;
}
//----------------------------------------------------------------------------------------------------


SRect  fighter::GetBoundingBox() const
{
	return mpCurrentState->GetBoundingBox();
}

//----------------------------------------------------------------------------------------------------

SRect  fighter::GetDamageBox() const
{
	return mpCurrentState->GetDamageBox();
}
//----------------------------------------------------------------------------------------------------

